
"use strict";

exports.qiniuObj = {
  bucket: "dyna-xb", //要上传的空间名
  imageUrl: "https://img.booxf.site/", // 空间绑定的域名
  accessKey: "0sjn7ky7jdaBgWAyR3jzMtFOGUM22ioA4ux7Ztwu", //Access Key
  secretKey: "3bCkkajhET6ABPnXGFgF8xCQdmVOyepbJ-M_SEBC", //Secret Key
};

exports.toInt = str => {
  if (typeof str === "number") return str;
  if (!str) return str;
  return parseInt(str, 10) || 0;
};

const sequelize = require("sequelize");
const Op = sequelize.Op;
/**
 * @name 封装模糊搜索元素
 * @params1 -Object- searchField -- 搜索对象，如{currentPage：1，pageSize： 10， operatorType：'好吧'}
 * @params2 -Array- removeField -- 剔除数组，如['currentPage', 'pageSize']
 * @params2 -Number- searchType -- 搜索类型，如1-精准搜索；2-模糊搜索（and）；3-模糊搜索（or）
 */
exports.fuzzySearch = ({
  searchField = {},
  removeField = [],
  fieldRequired = [],
  searchType = 1,
}) => {
  const baseField = { delAt: false };
  if (Object.prototype.toString.call(searchField) !== "[object Object]") {
    // 不为对象
    return baseField;
  }
  let searchKeys = Object.keys(searchField);
  if (searchKeys.length <= 0) {
    // 对象没有属性
    return baseField;
  }
  searchKeys = [...new Set(searchKeys)]; // 去重
  let outputContent;
  // 根据搜索类型走不同的步骤
  if (searchType === 1) {
    // 精准搜索
    let fieldAfterCleaning = {};
    searchKeys &&
      searchKeys.forEach(item => {
        if (removeField.indexOf(item) === -1) {
          fieldAfterCleaning[item] = searchField[item];
        }
      });
    if (Object.keys(fieldAfterCleaning).length <= 0) {
      // 没有精准搜索字段
      return baseField;
    }
    outputContent = fieldAfterCleaning;
  } else {
    // 模糊搜索
    let fieldAfterCleaning = [];
    searchKeys &&
      searchKeys.forEach(item => {
        if (removeField.indexOf(item) === -1) {
          const initialization = {};
          initialization[item] = { [Op.like]: `%${searchField[item] || ""}%` };
          fieldAfterCleaning.push(initialization);
        }
      });
    if (fieldAfterCleaning.length <= 0) {
      // 没有模糊搜索字段
      return baseField;
    }
    if (searchType === 2) {
      // 模糊搜索（and）
      outputContent = Object.assign(
        { [Op.and]: fieldAfterCleaning },
        baseField
      );
    } else {
      // 模糊搜索（or）
      outputContent = Object.assign({ [Op.or]: fieldAfterCleaning }, baseField);
    }
  }

  return outputContent;
};

// 鉴权白名单 -- 加入白名单后将不会被鉴定登录权限
exports.whitelist = [
  "/tube/admin/login",
  "/tube/admin/test",
  "/tube/user/queryProductClass",
  "/tube/user/queryProduct",
  "/tube/user/searchProduct",

  "/tube/wa/queryGoodsAll",



  //用户信息管理
  "/tube/user/register",
  "/tube/user/login",
  "/tube/user/queryUsers",
  "/tube/user/updateUsers",
  "/tube/admin/upload",
  "/tube/user/deleteUsers",
  //用户余额查询
  '/tube/user/queryUserBalance',
  //用户订单操作
  '/tube/user/operateOrders',
  //用户订单立即支付
  '/tube/user/payOrders',
  // 用户订单删除
  '/tube/user/deleteOrders',

  //Web端用户购物车查询
  '/tube/admin/queryUsersCart',
  //Web端用户购物车更新
  '/tube/admin/updateUsersCart',
  "/tube/user/queryShoppings",

  //wc删除购物车单个商品
  '/tube/user/deleteOneShoppings',
  "/tube/user/deleteManyShoppings",
  '/tube/user/createOrders',
  '/tube/user/createShopping',
  //预结算商品查询
  '/tube/user/queryJiesProducts',

  //用户地址查询
  '/tube/user/queryUsersAddress',
  //用户地址新增
  '/tube/user/createUsersAddress',
  //用户地址修改
  '/tube/user/updateUsersAddress',
  //设置用户默认地址
  '/tube/user/setdefaultAddress',
  //用户默认地址查询
  '/tube/user/queryDefAddr',
  //用户地址删除
  '/tube/user/deleteUsersAddress',

  //用户订单查询
  '/tube/user/queryOrders',
  '/tube/user/queryOrdersNew',

  // 商品信息管理
  "/tube/admin/queryProducts",
  "/tube/admin/createProducts",
  "/tube/admin/updateProducts",
  "/tube/admin/deleteProducts",
  //首页商品搜索
  '/tube/wa/searchProducts',
  //商品详情查询
  '/tube/wa/detailProduct',

  //商品分类管理1
  "/tube/admin/queryClasses",
  "/tube/admin/createClasses",
  "/tube/admin/updateClasses",
  "/tube/admin/deleteClasses",
  "/tube/wa/queryAllClasses",
  "/tube/admin/queryClassAll",
  "/tube/admin/queryAll",

   //依据二级分类查询商品
  "/tube/wa/querySecondProducts",

  //优惠券管理1
  "/tube/admin/queryCoupons",
  "/tube/admin/createCoupons",
  "/tube/admin/updateCoupons",
  "/tube/admin/deleteCoupons",

  //用户优惠券管理
  //查询用户拥有的优惠券
  "/tube/user/queryUserCoupons",
  //用户领取优惠券
  "/tube/user/createUserCoupons",
  //修改用户拥有的优惠券的状态
  "/tube/user/updateUserCoupons",

  //商品订单管理1
  "/tube/admin/queryOrders",
  '/tube/admin/updateOrders',
  '/tube/admin/deleteOrders',


  //轮播图管理
  "/tube/admin/queryCarousels",
  "/tube/admin/createCarousels",
  "/tube/admin/updateCarousels",
  "/tube/admin/deleteCarousels",
  "/tube/wa/queryCarousels",

  //活动管理
  "/tube/admin/queryActivitys",
  "/tube/admin/createActivitys",
  "/tube/admin/updateActivitys",
  "/tube/admin/deleteActivitys",
  "/tube/wa/queryActivitys",
  //挂载活动商品
  "/tube/admin/selectActivitysProducts",
  //移除活动商品
  '/tube/admin/removeActivitysProducts',
];

exports.EVENT = {
  SERVER_ADDRESS: "http://127.0.0.1:7001",
};
